//
//  UIBarButtonItem+Helper.swift
//  SwiftWeibo
//
//  Created by 苏强 on 14/11/18.
//  Copyright © 2014年 suqianghotel. All rights reserved.
//

import UIKit

extension UIBarButtonItem
{
    
    static var space : UIBarButtonItem {
        let spacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        spacer.width = -4;
        return spacer
        
        
    }
    
    public convenience init(title :String?,
                            colorHighlight: UIColor?,
                            colorNormal : UIColor?,
                            size: CGFloat?,
                     imageName: String?,
                     highlighted: String?,
                     selected: String?,
                     target:Any?,
                     action:Selector?)
    {
        
        let button = UIButton()
        
        if let titleText = title
        {
            button.setTitle(titleText, for: .normal)
        }
        
        
        if let colorValue = colorHighlight {
            button.setTitleColor(colorValue, for: .highlighted)
        }
        else
        {
            button.setTitleColor(UIColor.orange, for: .highlighted)
        }
        
        if let colorNormalValue = colorNormal {
            button.setTitleColor(colorNormalValue, for: .normal)
        }
        else
        {
            button.setTitleColor(UIColor.gray, for: .normal)
        }
        
        
        if let sizeValue = size {
            button.titleLabel?.font = UIFont.systemFont(ofSize: sizeValue)
        }
        else
        {
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        }
        
        if let selectedValue = selected {
            button.setImage(UIImage(named:selectedValue), for: .selected)
        }
        
        
        if let imageNameValue = imageName
        {
            button.setImage(UIImage(named:imageNameValue), for: .normal)
        }
        
        if let imageNameValue = highlighted {
            button.setImage(UIImage(named:imageNameValue), for: .highlighted)
        }
        

        
        if let actionValue = action {
            button.addTarget(target, action: actionValue, for: .touchUpInside)
        }
        
        
        button.sizeToFit()
        
        self.init(customView:button)
    }
    
    
    
    public convenience init(imageName: String?,
                            selected: String?,
                            target:Any?,
                            action:Selector?)
    {
        self.init(title: nil,
                  colorHighlight: nil,
                  colorNormal : nil,
                  size: nil,
                  imageName: imageName,
                  highlighted: nil,
                  selected: selected,
                  target:target,
                  action:action)
    }
    
    public convenience init(imageName: String?,
                            highlighted: String?,
                            target:Any?,
                            action:Selector?)
    {
        self.init(title: nil,
                colorHighlight: nil,
                colorNormal : nil,
                size: nil,
                imageName: imageName,
                highlighted: highlighted,
                selected: nil,
                target:target,
                action:action)
    }
    
    
    public convenience init(title :String?,
                            colorHighlight: UIColor?,
                            colorNormal : UIColor?,
                            target:Any?,
                            action:Selector?)
    {
        self.init(  title :title,
                    colorHighlight: colorHighlight,
                    colorNormal : colorNormal,
                    size: 14,
                    imageName: nil,
                    highlighted: nil,
                    selected: nil,
                    target:target,
                    action:action)
    }
    
    public convenience init(title :String?,
                            target:Any?,
                            action:Selector?)
    {
        self.init(  title :title,
                    colorHighlight: UIColor.orange,
                    colorNormal : UIColor.gray,
                    size: 14,
                    imageName: nil,
                    highlighted: nil,
                    selected: nil,
                    target:target,
                    action:action)
    }
    
    
    public convenience init(title :String?,
                            imageName: String?,
                            highlighted: String?,
                            target:Any?,
                            action:Selector?)
    {
        self.init(  title :title,
                    colorHighlight: UIColor.orange,
                    colorNormal : UIColor.gray,
                    size: 14,
                    imageName: imageName,
                    highlighted: highlighted,
                    selected: nil,
                    target:target,
                    action:action)
    }
    
   
}
